//结束或暂停游戏
function stopGame() {
    bg.xspeed = 0
    land.xspeed = 0

    //小鸟yspeed = 0  重力 =  0
    bird.yspeed = 0
    bird.g = 0
    //小鸟翅膀停止煽动
    bird.stopSwing()
    //禁用小鸟jump()
    bird.flag = false

    //停止水管产生
    producer.stopProduce()
    //让每一个水管停止移动
    producer.pairs.forEach(function (pair) {
        pair.upPipe.xspeed = 0
        pair.downPipe.xspeed = 0
    })
}

//继续游戏
function startGame() {
    bg.xspeed = -100
    land.xspeed = -100
    //恢复重力
    bird.g = 1500

    //小鸟翅膀开始煽动
    bird.startSwing()
    //开启小鸟jump()
    bird.flag = true

    //开启水管产生
    producer.startProduce()
    //让每一个水管开始移动
    producer.pairs.forEach(function (pair) {
        pair.upPipe.xspeed = -100   //速度
        pair.downPipe.xspeed = -100
    })
}

//按下空格键，小鸟跳跃
document.addEventListener("keydown", function (event) {
    if (event.keyCode === 32) {
        bird.jump()
    }
})
//点击game窗口，小鸟跳跃
gameDom.addEventListener("click", function () {
    bird.jump()
})

//弹窗相关
function openwindow() {
    document.querySelector('dialog').showModal();
}
function closewindow() {
    document.querySelector('dialog').close();
}

//暂停游戏按钮
const stop = document.querySelector(".stop")
stop.addEventListener("click", function () {
    stopGame()
    openwindow()
})

//继续游戏按钮
const continueBtn = document.querySelector(".continue")
continueBtn.addEventListener("click", function () {
    closewindow()
    startGame()
})

//重玩按钮
const restart = document.querySelector(".restart")
restart.addEventListener("click", function () {
    location.reload()
})

//返回主页
const back = document.querySelector(".back")
back.addEventListener("click", function () {
    // location.href = "../../main/index.html"
    history.back()
})